package com.example.mybatispractice.mapper;

import com.example.mybatispractice.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-15
 * Time: 18:55
 */
@Mapper
public interface UserInfoMapper {
    /*@Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime," +
            " update_time as updateTime from user_info")*/
   /* @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })*/
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info")
    List<UserInfo> selectAll();

    /*@ResultMap("BaseMap")*/
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info where id = #{id}")
    UserInfo selectUserById(Integer id);

    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info where id = ${id}")
    UserInfo selectUserById1(Integer id);

    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info where username = '${name}'")
    UserInfo selectUserByName(String name);

    @Select("select * from user_info where username = '${name}' and password = '${password}'")
    List<UserInfo> selectUserByNameAndPassword(String name,String password);

    /*@Select("select * from user_info where gender = #{gender} and age = #{age}")
    List<UserInfo> selectGenderAndAge(Integer age,Integer gender);*/

    /*@Select("select * from user_info where gender = #{param2} and age = #{param1}")
    List<UserInfo> selectGenderAndAge(Integer age,Integer gender);*/

    @Select("select * from user_info where gender = #{p2} and age = #{p1}")
    List<UserInfo> selectGenderAndAge(@Param("p1") Integer age, @Param("p2")Integer gender);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username,password,age,gender) values (#{userName},#{passWord}," +
            "#{age},#{gender})")
    Integer insertUser(UserInfo userInfo);

    /*@Insert("insert into user_info (username,password,age,gender) values (#{userInfo.userName}," +
            "#{userInfo.passWord},#{userInfo.age},#{userInfo.gender})")
    Integer insertUser(@Param("userInfo") UserInfo userInfo);*/

    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);

    @Update("update user_info set password = #{password} where id = #{id}")
    Integer update(Integer id,String password);

    @Update("update user_info set password = #{passWord} where id = #{id}")
    Integer update1(UserInfo userInfo);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

    @Select("select * from user_info where username like '%${name}%'")
    List<UserInfo> selectUserByLike(String name);

    @Select("select * from user_info where username like CONCAT('%',#P{name},'%') ")
    List<UserInfo> selectUserByLikeConcat(String name);

    @Insert("<script>" +
            "insert into user_info" +
            " <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            "<if test=\"userName!=null\"> userName ,</if>" +
            "<if test=\"passWord!=null\"> passWord ,</if>" +
            "<if test=\"age!=null\"> age ,</if>" +
            "<if test=\"gender!=null\"> gender ,</if>" +
            "</trim> values" +
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            "<if test=\"userName!=null\">  #{userName} ,</if>" +
            "<if test=\"passWord!=null\"> #{passWord} ,</if>" +
            "<if test=\"age!=null\"> #{age} ,</if>" +
            "<if test=\"gender!=null\"> #{gender} ,</if>" +
            " </trim>"+
            "</script>")
    Integer insertUserByCondition(UserInfo userInfo);
}
